package com.base.pig.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName intelligent-city
 * @Package com.pig4cloud.pig.lab.util
 * @ClassName DateUtils
 * @Description 时间函数处理
 * @Company lab
 * @Author wangw
 * @Date 2020/6/29 0029 14:45
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
public class DateUtils {

    /**
     * @param month : 月份数
     * @return
     * @Title 获取当前时间前几个月
     * @Description
     * @author wangw
     * @date 2020/6/29 0029 14:15
     */
    public static String getLastMonths(int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -month);
        Date m = c.getTime();
        return sdf.format(m);
    }
    
    /** 
     * @Title 获取年份
     * @Description
     * @param lastYearCount : 过去年数,表示过去x年
     * @return
     * @author wangw
     * @date 2020/7/2 0002 11:11
    */
    public static String getLastYear(int lastYearCount){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        return format.format(y);
    }
    /**
     * 获取本月第一天
     * @return
     */
    public static Date getmindate(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        return calendar.getTime();
    }

    /**
     * 获取本月最后一天
     * @return
     */
    public static Date getmaxdate(){
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar2.getTime();
    }

    /**
     * 获取某年某月的第一天
     * @Title:getFisrtDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getFisrtDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }



    /**
     * 获取某月的最后一天
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }


    /**
     * 得到两个日期之间的所有日期
     * @param startTime   yyyy-mm-dd
     * @param endTime      yyyy-mm-dd
     * @return
     */
    public static List<String> getBetweenDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getNowTime()
    {
        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);

        return dateNowStr;
    }

    /**
     * 获取当前日期
     * @return
     */
    public static String getNowDay()
    {
        Date d = new Date();
        System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(d);

        return dateNowStr;
    }

    /**
     * 获取昨天
     * @return
     */
    public static String getyestoday()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //得到前一天
        Date date = calendar.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(date));

        return df.format(date);
    }

    /**
     * 获得截止到目前，今天一共过了多少秒
     * @return
     */
    public static int getSeconds() {
        Calendar now = Calendar.getInstance();
        //System.out.println("年: " + now.get(Calendar.YEAR));
        //System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
       // System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));
        System.out.println("秒: " + now.get(Calendar.SECOND));

        int  seconds=now.get(Calendar.HOUR_OF_DAY)*3600+now.get(Calendar.MINUTE)*60+now.get(Calendar.SECOND);
        return seconds;
    }



    /**
     *获取两个时间段中,start+interval*n的时间（同一天）
     * @param start  01
     * @param end   23
     * @param interval  1,2,6
     * @return
     */
    public static List<String> getIntervalHour(String start, String end,String interval) {

        List<String> array = new ArrayList<>();

        int sum=Integer.parseInt(start);

        if (sum < 10) {
            array.add( "0" +sum+":00");
        } else {
            array.add(start);
        }
        for (int i = 0; i < 24; i++) {

            sum = sum + Integer.parseInt(interval);

            if(sum>Integer.parseInt(end)){
                break;
            }

            if (sum < 10) {
                array.add( "0" +sum+":00");
            } else {
                array.add(sum+":00");
            }
        }
        return array;
    }


    /***
     *  获取两个时间之间的月份
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }


    public static void main(String[] args) throws ParseException {


       System.out.println(getMonthBetween("2021-12-01","2022-03-01"));
       // System.out.println( getTimeDay("6","23","6"));


        //String[] arr="01:11:33".split(":");

      //  System.out.println(arr[0]);
       // System.out.println(arr[1]);
      //  System.out.println(arr[2]);
    }



}